﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CdcSoftware.Pivotal.Engine.Client.ClientTasks.Attributes;
using CdcSoftware.Pivotal.Applications.Core.Client;
using System.Data;
using CdcSoftware.Pivotal.Engine.Client.Services.Interfaces;
using System.Windows.Forms;
using CdcSoftware.Pivotal.Engine;
using CdcSoftware.Pivotal.Applications.Core.Common;

namespace Pcm.Retail.Client.FormTasks.CommandHandlers
{
    public class RE_BatchCommandHandlers: CommandClientTask
    {
        [ClientTaskCommand]
        public virtual void CurrentBatchCommand()
        {
            try
            {
                //if current batch record is already open, close it first as there is an issue with refresh
                ICenterContentView centerContent = (ICenterContentView)ClientContext.ClientAPIService.GetActiveContentObject();
                if (centerContent is IFormView)
                {
                    if (this.FormView.FormName == "RE_Batch")
                    {
                        if (this.FormView.Dirty)
                        {
                            DialogResult res =  PivotalMessageBox.Show("Changes to the batch will be lost if you exit. Click OK to exit, or Cancel to stay on the current record.", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                            if (res == DialogResult.Cancel)
                                return;                            
                        }
                        this.FormView.CloseForm(false);
                    }
                }
                //RA:17/Aug/2012:Change for Mobile System:Start:
                DataTable currentBatch = Globals.GetDataTable("RE_In Progress Batch for Current Store for today?", new object[] { }, "Batch_Status", "RE_Batch_Id");
                //DataTable currentBatch = Globals.GetDataTable("RE_In Progress Batch for Current Store", new object[] { }, "Batch_Status", "RE_Batch_Id");
                //RA:17/Aug/2012:Change for Mobile System:End:

                if (currentBatch != null && currentBatch.Rows.Count == 0)
                    PivotalMessageBox.Show("There is no batch currently In Progress.", MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

                else if (currentBatch != null && currentBatch.Rows.Count == 1)
                    Globals.ShowForm("RE_Batch", Id.Create(currentBatch.Rows[0]["RE_Batch_Id"]));
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc);
            }
        }
        [ClientTaskCommand]
        public virtual void NewBatchCommand()
        {
            try
            {
                //RA:16/Aug/2012:Change for Mobile System:Start:
                bool masterPOS = false;
                Id storePOSId = Id.Create(Globals.ServerSqlIndex("Employee", "RE_Store_POS_Id", this.SystemClient.UserProfile.EmployeeId));

                if (storePOSId != null)
                    masterPOS = TypeConvert.ToBoolean(Globals.ServerSqlIndex("RE_Store_POS", "Master_POS", storePOSId));
                if (!masterPOS)
                {
                    PivotalMessageBox.Show("Batch can only be created on the Master POS. Please try on the Terminal designated as Master POS.",
                            MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    //this.FormView.CloseForm(true);
                    return;
                }
                Globals.NewForm("RE_Batch", null);
                //RA:16/Aug/2012:Change for Mobile System:End:
            }
            catch (Exception exc)
            {
                Globals.HandleException(exc);
            }
        }
    }
}
